//
//  YSButtonCategoryViewController.m
//  YSKitDemo
//
//  Created by Kyson on 2018/12/13.
//  Copyright © 2018 YangShen. All rights reserved.
//

#import "YSButtonCategoryViewController.h"
#import "UIColor+YSExtension.h"
#import "UIButton+YSLayout.h"
#import "UIButton+YSEventInterval.h"

@interface YSButtonCategoryViewController ()

@end

static CGFloat kMargin = 10;

@implementation YSButtonCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // 1. 按钮文字图片位置
    CGFloat startY = kMargin;
    startY = [self setupButtonLayout:startY];
    
    // 2. 防止重复点击
    startY = [self setupTapButton:startY];
    
}

- (CGFloat)setupTapButton:(CGFloat)startY {
    CGFloat viewH = 90;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, startY, self.view.ys_width, viewH)];
    view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:view];
    
    CGFloat labelH = 30;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.ys_width-30, 30)];
    label.text = @"防止重复点击按钮";
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,  labelH, view.ys_width, kGlobalLineH)];
    lineView.backgroundColor = kGlobalLineColor;
    [view addSubview:lineView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(15, 40, view.ys_width - 30, 40);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor ys_randomColor];
    [button setTitle:@"防止重复点击" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tapButtonClick) forControlEvents:UIControlEventTouchUpInside];
    button.ys_eventInterval = 2;
    [view addSubview:button];
    
    return startY + viewH + kMargin;
}

- (void)tapButtonClick {
    NSLog(@"tap button...");
}

- (CGFloat)setupButtonLayout:(CGFloat)startY {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, startY, self.view.ys_width, 0)];
    view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:view];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.ys_width-30, 30)];
    label.text = @"按钮布局";
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame), view.ys_width, kGlobalLineH)];
    lineView.backgroundColor = kGlobalLineColor;
    [view addSubview:lineView];
    
    CGFloat w = (view.ys_width - 10 * 3 - 30)/4;
    CGFloat h = 60;
    UIImage *image = [UIImage imageNamed:@"ff_Icon"];
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(15 + (w + 10) * i, 40, w, h);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor ys_randomColor];
        if (i == 0) {
            [button setImage:image title:@"文字在左" titlePosition:YSTitlePositionLeft spacing:10];
        } else if (i == 1) {
            [button setImage:image title:@"文字在右" titlePosition:YSTitlePositionRight spacing:10];
        } else if (i == 2) {
            [button setImage:image title:@"文字在上" titlePosition:YSTitlePositionTop spacing:5];
        } else {
            [button setImage:image title:@"文字在下" titlePosition:YSTitlePositionBottom spacing:5];
        }
        [button addTarget:self action:@selector(tapButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    
    view.ys_height = CGRectGetMaxY(label.frame) + h + kMargin;
    
    return startY + view.ys_height + kMargin;
}

@end
